
package com.android.finaltracer.model;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;

public class Setting {

    private Context mContext;
    private TelephonyManager mPhone;
    // private String associatedPhoneNumber;
    private SharedPreferences mPrefs;

    public Setting(Context context) {
        mContext = context;
        if (mPrefs == null) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        }
    }

    public String getSelfNumber() {
        String result = "";
        if (mPrefs == null) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        }
        if (null == mPrefs) {
            return result;
        }
        result = mPrefs.getString(FTSharePreference.SHARE_KEY_SELF_NUMBER, "");
        return result;
    }

    public boolean setSelfNumber(String selfNumber) {
        boolean result = false;
        if (mPrefs == null) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        }
        if (null == mPrefs) {
            return result;
        }
        if (selfNumber == null) {
            selfNumber = "";
        }
        try {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putString(FTSharePreference.SHARE_KEY_SELF_NUMBER,
                    selfNumber);
            editor.apply();
            result = true;
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    public String getOldImsi() {
        String result = "";
        if (mPrefs == null) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        }
        if (null == mPrefs) {
            return result;
        }
        result = mPrefs.getString(FTSharePreference.SHARE_KEY_OLD_IMSI, "");
        return result;
    }

    public boolean setOldImsi(String oldImsi) {
        boolean result = false;
        if (mPrefs == null) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        }
        if (null == mPrefs) {
            return result;
        }
        if (oldImsi == null) {
            oldImsi = "";
        }
        try {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putString(FTSharePreference.SHARE_KEY_OLD_IMSI, oldImsi);
            editor.apply();
            result = true;
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    /*
     * public String getAssociatedPhoneNum() { String result = ""; if (mPrefs ==
     * null) { mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
     * } if (null == mPrefs) { return result; } result = mPrefs.getString(
     * FTSharePreference.SHARE_KEY_ASSOCIATED_PHONE_NUMBER, ""); return result;
     * }
     */

    public String getAssociatedPhoneNum(int count) {
        String result = "";
        if (mPrefs == null) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        }
        switch (count) {
            case 1:
                result = mPrefs.getString(
                        FTSharePreference.SHARE_KEY_ASSOCIATED_PHONE_NUMBER1, "");
                break;
            case 2:
                result = mPrefs.getString(
                        FTSharePreference.SHARE_KEY_ASSOCIATED_PHONE_NUMBER2, "");
                break;
            case 3:
                result = mPrefs.getString(
                        FTSharePreference.SHARE_KEY_ASSOCIATED_PHONE_NUMBER3, "");
                break;
            case 4:
                result = mPrefs.getString(
                        FTSharePreference.SHARE_KEY_ASSOCIATED_PHONE_NUMBER4, "");
                break;
            case 5:
                result = mPrefs.getString(
                        FTSharePreference.SHARE_KEY_ASSOCIATED_PHONE_NUMBER5, "");
                break;
            default:
                result = "";
        }

        return result;
    }

    /*
     * public boolean setAssociatedPhoneNum(String phoneNumber) { boolean result
     * = false; if (mPrefs == null) { mPrefs =
     * PreferenceManager.getDefaultSharedPreferences(mContext); } if (null ==
     * mPrefs) { return result; } if (phoneNumber == null) { phoneNumber = ""; }
     * try { SharedPreferences.Editor editor = mPrefs.edit(); editor.putString(
     * FTSharePreference.SHARE_KEY_ASSOCIATED_PHONE_NUMBER, phoneNumber);
     * editor.apply(); result = true; } catch (Exception e) { result = false; }
     * return result; }
     */
    public boolean setAssociatedPhoneNum(String phoneNumber, int count) {
        boolean result = false;
        if (mPrefs == null) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        }

        try {
            SharedPreferences.Editor editor = mPrefs.edit();
            switch (count) {
                case 1:
                    editor.putString(
                            FTSharePreference.SHARE_KEY_ASSOCIATED_PHONE_NUMBER1,
                            phoneNumber);
                    editor.apply();
                    result = true;
                    break;
                case 2:
                    editor.putString(
                            FTSharePreference.SHARE_KEY_ASSOCIATED_PHONE_NUMBER2,
                            phoneNumber);
                    editor.apply();
                    result = true;
                    break;
                case 3:
                    editor.putString(
                            FTSharePreference.SHARE_KEY_ASSOCIATED_PHONE_NUMBER3,
                            phoneNumber);
                    editor.apply();
                    result = true;
                    break;
                case 4:
                    editor.putString(
                            FTSharePreference.SHARE_KEY_ASSOCIATED_PHONE_NUMBER4,
                            phoneNumber);
                    editor.apply();
                    result = true;
                    break;
                case 5:
                    editor.putString(
                            FTSharePreference.SHARE_KEY_ASSOCIATED_PHONE_NUMBER5,
                            phoneNumber);
                    editor.apply();
                    result = true;
                    break;
                default:
                    editor.apply();
                    result = false;
            }

        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    public String getImeiStr() {
        String result = "";
        if (mContext == null)
            return result;
        if (mPhone == null) {
            mPhone = (TelephonyManager) mContext
                    .getSystemService(Context.TELEPHONY_SERVICE);
        }
        result = mPhone.getDeviceId();
        return result;
    }

    public String getOldImei() {
        String result = "";
        if (mPrefs == null) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        }
        if (null == mPrefs) {
            return result;
        }
        result = mPrefs.getString(FTSharePreference.SHARE_KEY_OLD_IMEI, "");
        return result;
    }

    public boolean setOldImei(String oldImei) {
        boolean result = false;
        if (mPrefs == null) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        }
        if (oldImei == null) {
            oldImei = "";
        }
        try {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putString(FTSharePreference.SHARE_KEY_OLD_IMEI, oldImei);
            editor.apply();
            result = true;
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

}
